﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ESupervision.Models;
using Microsoft.AspNet.Identity;

namespace ESupervision.Controllers
{
    [Authorize(Roles = "Teacher")]
    public class MeetingNoteController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: /MeetingNote/
        public async Task<ActionResult> Index(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MeetingEvent meetingevent = await db.MeetingEvents.FindAsync(id);
            if (meetingevent == null)
            {
                return HttpNotFound();
            }
            ViewBag.MEventID = id;
            var meetingnotes = db.MeetingNotes.Where(n => n.MeetingEventID == id).Include(m => m.MeetingEvent).Include(m => m.User);
            return View(await meetingnotes.ToListAsync());
        }

        // GET: /MeetingNote/Create
        public ActionResult Create(int id)
        {
            MeetingEvent meetingevent = db.MeetingEvents.Find(id);
            if (meetingevent == null)
            {
                return HttpNotFound();
            }
            MeetingNote meetingnote = new MeetingNote
            {
                MeetingEventID = id,
                UserID = User.Identity.GetUserId(),
            };
            return View(meetingnote);
        }

        // POST: /MeetingNote/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "MeetingEventID,UserID,Note")] MeetingNote meetingnote)
        {
            if (ModelState.IsValid)
            {
                meetingnote.CreatedTime = DateTime.Now;
                db.MeetingNotes.Add(meetingnote);
                await db.SaveChangesAsync();
                return RedirectToAction("Index", new { id = meetingnote.MeetingEventID });
            }
            return View(meetingnote);
        }

        // GET: /MeetingNote/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MeetingNote meetingnote = await db.MeetingNotes.FindAsync(id);
            if (meetingnote == null)
            {
                return HttpNotFound();
            }
            return View(meetingnote);
        }

        // POST: /MeetingNote/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "MeetingNoteID,MeetingEventID,UserID,CreatedTime,Note")] MeetingNote meetingnote)
        {
            if (ModelState.IsValid)
            {
                db.Entry(meetingnote).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index", new { id = meetingnote.MeetingEventID });
            }
            return View(meetingnote);
        }

        public async Task<ActionResult> Delete(int id)
        {
            MeetingNote meetingnote = await db.MeetingNotes.FindAsync(id);
            db.MeetingNotes.Remove(meetingnote);
            await db.SaveChangesAsync();
            return RedirectToAction("Index", new { id = meetingnote.MeetingEventID });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
